package com.newsee.remote.ecommerce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newsee.remote.common.database.entity.CrmPurveycompany;
import com.newsee.remote.common.database.entity.ShMaterial;
import com.newsee.remote.common.database.entity.ShMaterialPurveyCompany;
import com.newsee.remote.common.database.mapper.ShMaterialPurveyCompanyMapper;
import com.newsee.remote.ecommerce.service.IShMaterialPurveyCompanyService;
import com.newsee.remote.supplier.service.ICrmPurveycompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liangzhichen
 * @since 2019-08-12
 */
@Service
@Slf4j
public class ShMaterialPurveyCompanyServiceImpl extends ServiceImpl<ShMaterialPurveyCompanyMapper, ShMaterialPurveyCompany> implements IShMaterialPurveyCompanyService {

    @Autowired
    private ICrmPurveycompanyService crmPurveycompanyService;

    @Override
    public void saveRelate(ShMaterial material, String lifnr) {
        try {
            CrmPurveycompany crmPurveycompany = crmPurveycompanyService.findByLifnr(lifnr);
            if (null != crmPurveycompany) {
                Long materialId = material.getId();
                ShMaterialPurveyCompany shMaterialPurveyCompany = new ShMaterialPurveyCompany();
                shMaterialPurveyCompany.setPurveyID(crmPurveycompany.getId());
                shMaterialPurveyCompany.setMaterialID(materialId);
                shMaterialPurveyCompany.setPrice(material.getRefPrice());
                save(shMaterialPurveyCompany);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增供应商关联商品失败：供应商编码：" + lifnr);
        }
    }

    @Override
    public void updateRelate(Long materialId, String lifnr, BigDecimal price) {
        try {
            CrmPurveycompany crmPurveycompany = crmPurveycompanyService.findByLifnr(lifnr);
            if (null != crmPurveycompany) {
                QueryWrapper<ShMaterialPurveyCompany> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("MaterialID", materialId).eq("PurveyID", crmPurveycompany.getId());
                ShMaterialPurveyCompany shMaterialPurveyCompany = new ShMaterialPurveyCompany();
                update(shMaterialPurveyCompany, queryWrapper);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改供应商关联商品失败：供应商编码：" + lifnr);
        }
    }


}
